package com.lmoumou.lib_calendarview.widget

import android.content.Context
import android.graphics.Color
import android.support.v7.widget.RecyclerView
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.lmoumou.lib_calendarview.R
import com.lmoumou.lib_calendarview.entity.DayAttrsBeen
import com.lmoumou.lib_calendarview.entity.DayBeen
import kotlinx.android.synthetic.main.item_moth.view.*

/**
 * @author Lmoumou
 * @date : 2019/11/6 14:49
 */
class MothAdapter(
    private val context: Context, private val dataList: MutableList<DayBeen>,
    private val onItemClick: (data: DayBeen, position: Int) -> Unit
) :
    RecyclerView.Adapter<MothAdapter.MothViewHolder>() {

    private val mInflater: LayoutInflater by lazy { LayoutInflater.from(context) }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MothViewHolder {
        return MothViewHolder(mInflater.inflate(R.layout.item_moth, parent, false))
    }

    override fun getItemCount(): Int = dataList.size

    override fun onBindViewHolder(holder: MothViewHolder, position: Int) {
        dataList[position].run {
            val mMenstruationStartView = holder.itemView.mMenstruationStartView
//            mMenstruationStartView.visibility = if (isMothDay) View.VISIBLE else View.INVISIBLE
            holder.itemView.tvDate.visibility = if (this.isMothDay) View.VISIBLE else View.INVISIBLE
            holder.itemView.tvDate.text = this.day
            this.dayAttrs?.let {
                bindAttrs(holder, it, mMenstruationStartView)
            }
            holder.itemView.setOnClickListener {
                onItemClick(this, position)
            }
        }
    }

    private fun bindAttrs(
        holder: MothViewHolder,
        dayAttrs: DayAttrsBeen,
        mMenstruationStartView: MenstruationStartView
    ) {
        dayAttrs.run {

            val tvDate = holder.itemView.tvDate
            val ivSubscript = holder.itemView.ivSubscript
            if (asEasyPregnancy) {
                ivSubscript.setImageResource(R.mipmap.ic_test2)
            } else {
                ivSubscript.setImageResource(R.drawable.defult_subscript)
                ivSubscript.setImageResource(if (stage == 2) R.mipmap.ic_test else R.drawable.defult_subscript)
            }

            if (stage==2){
                when (menstruationType) {
                    1 -> {
                        mMenstruationStartView.visibility = View.VISIBLE
                        mMenstruationStartView.setType(1)
                    }
                    2 -> {
                        mMenstruationStartView.visibility = View.VISIBLE
                        mMenstruationStartView.setType(2)
                    }
                    3 -> {
                        mMenstruationStartView.visibility = View.VISIBLE
                        mMenstruationStartView.setType(3)
                    }
                    else -> mMenstruationStartView.visibility = View.INVISIBLE
                }
            }

            if (isSelect) {
                tvDate.setBackgroundResource(R.drawable.item_bg_select)
                tvDate.setTextColor(Color.parseColor("#FFFFFF"))
            } else {
                tvDate.setBackgroundResource(R.drawable.item_bg_normal)
                when (stage) {
                    1 -> {//安全期
                        tvDate.setTextColor(Color.parseColor("#59E8D8"))
//                        ivSubscript.setImageResource(R.drawable.defult_subscript)
                    }
                    2 -> {//月经期
                        tvDate.setTextColor(Color.parseColor("#FE7E6D"))

//                        ivSubscript.setImageResource(R.mipmap.ic_test)
                    }
                    3 -> {//预测月经期
                        tvDate.setTextColor(Color.parseColor("#FF8DC5"))
//                        ivSubscript.setImageResource(R.drawable.defult_subscript)
                    }
                    4 -> {//排卵期
                        tvDate.setTextColor(Color.parseColor("#A4A8FF"))
//                        ivSubscript.setImageResource(R.drawable.defult_subscript)
                    }
                    else -> {
                        tvDate.setTextColor(Color.parseColor("#9C9EA8"))
//                        ivSubscript.setImageResource(R.drawable.defult_subscript)
                    }
                }

            }
        }
    }

    inner class MothViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView)
}